"""__author__=Hanlin"""
import pymysql

# 获取数据库连接
def get_connection():
    host = '127.0.0.1'
    port = 3306
    user = 'root'
    password = 'zhl901010'
    database = 'maoyan'
    db = pymysql.connect(host, user, password, database, charset='utf8', port=port)
    return db

# 获取游标
def get_cursor(con):
    cursor = con.cursor()
    return cursor

# 关闭数据库连接
def close_connection(con):
    con.close()

# 插入数据
# def save_db(con, cursor, data_dict):
#     sql = 'insert into movie (title, releasetime, actor, ranks, score, cover) values ("%s", "%s", "%s", "%s", "%s", "%s")' % (data_dict['title'], data_dict['releasetime'], data_dict['actor'], data_dict['rank'], data_dict['score'], data_dict['cover'])
#     print(sql)
#     cursor.execute(sql)
#     con.commit()

# 可以避免一些sql的注入问题
def save_db(con, cursor, data_dict):
    sql = 'insert into movie (title, releasetime, actor, ranks, score, cover) values (%s, %s, %s, %s, %s, %s)'
    print(sql)
    cursor.execute(sql, (
    data_dict['title'], data_dict['releasetime'], data_dict['actor'], data_dict['rank'], data_dict['score'],
    data_dict['cover']))
    con.commit()